//
//  WXYScrollView.m
//
//  Copyright © 2016年 hskj. All rights reserved.
//

#import "WXYScrollView.h"
#define Vwidth self.bounds.size.width
#define Vheight self.bounds.size.height
/**
 思路:
    在UIScrollView上放置一个UIImageView来控制对图片的缩放
 */
@interface WXYScrollView()
@property(nonatomic,strong)UIImageView *imgView;

@property(nonatomic,assign)BOOL isScale;
@property(nonatomic,strong)UIImage *imgs;
@end
@implementation WXYScrollView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        _imgView = [[UIImageView alloc]initWithFrame:self.bounds];
        [self addSubview:_imgView];
        _imgView.contentMode = UIViewContentModeScaleAspectFit;
        _imgView.userInteractionEnabled = YES;
        self.delegate = self;
        self.maximumZoomScale = 2.5f;
        self.minimumZoomScale = 1.0f;
        
        UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick)];

        UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleClick)];
        tap2.numberOfTapsRequired = 2;
        
        [tap1 requireGestureRecognizerToFail:tap2];
        
        [_imgView addGestureRecognizer:tap1];
        [_imgView addGestureRecognizer:tap2];
        _isScale = NO;
    }
    return self;
}

#pragma mark - 单击
-(void)tapClick
{
    self.didTap();
}

#pragma mark - 双击
-(void)doubleClick
{
    //判断双击的动作是放大还是缩小
    if (!_isScale)
    {
        [UIView animateWithDuration:0.5 animations:^{
             self.zoomScale = 2.0f;
        }];
       
    }else
    {
        [UIView animateWithDuration:0.5 animations:^{
             self.zoomScale = 1.0f;
        }];
       
    }
    
    _isScale = !_isScale;
   
}

#pragma mark - 加载图片
-(void)setImg:(UIImage *)img
{
    self.imgs = img;
    _isScale = NO;
    CGSize size = img.size;
    float scaleX = size.width / self.frame.size.width;
    float scaleY = size.height / self.frame.size.height;
    CGRect frame;
    //计算image的位置以及缩放比例
    if (scaleX < 1  && scaleY < 1)
    {
        frame = CGRectMake(Vwidth * 0.5 - size.width * 0.5, Vheight * 0.5 - size.height * 0.5, size.width, size.height);
    }else if (scaleY > 1 && scaleX < 1)
    {
        frame = CGRectMake(Vwidth *0.5  - img.size.width / scaleY * 0.5, 0, img.size.width / scaleY, Vheight);
    }else if (scaleY < 1 && scaleX > 1)
    {
          frame = CGRectMake(0, Vheight * 0.5 - img.size.height / scaleX * 0.5, Vwidth, img.size.height / scaleX);
    }else
    {
        if(scaleX < scaleY) frame = CGRectMake(Vwidth / 2 - size.width / scaleY /2, 0, size.width / scaleY, Vheight);
        else frame = CGRectMake(0, Vheight * 0.5 - size.height / scaleX * 0.5, Vwidth, size.height / scaleX);
    }
    _imgView.image = img;
    _imgView.frame = frame;
    self.contentSize = CGSizeZero;
}

#pragma mark - UIScrollView 代理
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return _imgView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    CGSize boundsSize = scrollView.bounds.size;
    CGRect imgFrame = _imgView.frame;
    
    CGSize contentSize = scrollView.contentSize;
    
    CGPoint centerPoint = CGPointMake(contentSize.width / 2, contentSize.height / 2);
    
    // center horizontally
    if (imgFrame.size.width <= boundsSize.width)
    {
        centerPoint.x = boundsSize.width / 2;
    }
    
    // center vertically
    if (imgFrame.size.height <= boundsSize.height)
    {
        centerPoint.y = boundsSize.height / 2;
    }
    _imgView.center = centerPoint;
}
@end
